from django.db import models
from django.utils.safestring import mark_safe
from stdimage import StdImageField
from ckeditor_uploader.fields import RichTextUploadingField
from dingxiang.utils.models import BaseModel
from users.models import UserModel

# Create your models here.

class ProvinceModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='省份名称')

    class Meta:
        db_table = 'province'
        verbose_name = '省份表'
        verbose_name_plural = verbose_name



class CityModel(BaseModel):
    province = models.ForeignKey('ProvinceModel', on_delete=models.CASCADE, related_name='cities',
                                 verbose_name='所属省份')
    name = models.CharField(max_length=20, verbose_name='城市名称')
    is_hot = models.BooleanField(default=False, verbose_name='是否热门城市')

    class Meta:
        db_table = 'city'
        verbose_name = '城市表'
        verbose_name_plural = verbose_name


# 主表
# 科室分类
class DepartmentCategoryModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='科室分类名称')
    remark = models.TextField(default='', blank=True, null=True, verbose_name='备注信息')

    class Meta:
        db_table = 'hospital_department_category'
        verbose_name = '科室分类表'
        verbose_name_plural = verbose_name

# 主表
# 检查手术
class OperationCategoryModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='分类名称')
    # 简介
    remark = models.TextField(default='', blank=True, null=True, verbose_name='备注信息')

    class Meta:
        db_table = 'operation_category'
        verbose_name = '检查手术分类表'
        verbose_name_plural = verbose_name

# 从表
class OperationModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='检查手术名称')
    description = RichTextUploadingField(default='', blank=True, null=True, verbose_name='介绍')
    departments = models.ForeignKey('DepartmentModel', on_delete=models.DO_NOTHING, related_name='operations',
                                    blank=True, null=True, verbose_name='所属科室')
    category = models.ForeignKey('OperationCategoryModel', on_delete=models.DO_NOTHING, related_name='operations',
                                 blank=True, null=True, verbose_name='所属分类')
    remark = models.CharField(max_length=256, blank=True, null=True, verbose_name='备注信息')
    class Meta:
        db_table = 'operation'
        verbose_name = '检查手术表'
        verbose_name_plural = verbose_name

# 从表
class DepartmentModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='科室名称')
    remark = models.TextField(default='', blank=True, null=True, verbose_name='备注信息')
    recommend_home_hot = models.BooleanField(default=False, verbose_name='是否推荐到首页最热栏目')
    category = models.ForeignKey('DepartmentCategoryModel', on_delete=models.DO_NOTHING, related_name='departments',
                                 blank=True, null=True, verbose_name='所属分类')

    class Meta:
        db_table = 'hospital_department'
        verbose_name = '科室表'
        verbose_name_plural = verbose_name

# 从表
class HospitalModel(BaseModel):
    LEVEL_CHOICES = (
        (1, '三甲'),
        (2, '二甲'),
        (3, '一乙'),
        (4, '其他'),
    )
    name = models.CharField(max_length=50, verbose_name='医院名称')
    level = models.SmallIntegerField(choices=LEVEL_CHOICES, default=4, verbose_name='医院等级')
    province = models.ForeignKey('ProvinceModel', on_delete=models.DO_NOTHING, related_name='hospitals',
                                 verbose_name='所属省份')
    city = models.ForeignKey('CityModel', on_delete=models.DO_NOTHING, related_name='hospitals',
                             verbose_name='所属城市')
    address = models.CharField(max_length=256, verbose_name='医院地址')
    phone = models.CharField(max_length=20, verbose_name='医院联系电话')
    covers = StdImageField(upload_to='hospital/', blank=True, null=True, verbose_name='医院封面'
                           , variations={
            'thumb_600x400': {'width': 600, 'height': 800},
            'thumb_300x200': {'width': 300, 'height': 400},
            'thumb_100x100': {'width': 100, 'height': 100}
        })
    description = RichTextUploadingField(default='', blank=True, null=True, verbose_name='医院简介')
    departments = models.ManyToManyField('DepartmentModel', related_name='hospitals', verbose_name='科室')

    class Meta:
        db_table = 'hospital'
        verbose_name = '医院表'
        verbose_name_plural = verbose_name

    def covers_small(self):
        if self.covers:
            return mark_safe(f"<img alt='covers' src='{self.covers.thumb_100x100.url}' />")
        return ''

    covers_small.short_description = '医院封面预览'
    covers_small.allow_tags = True
    covers_small.admin_order_field = 'covers'

# 从表
# 医生评价
class DoctorEvaluationModel(BaseModel):
    doctor = models.ForeignKey('DoctorModel', on_delete=models.DO_NOTHING, related_name='evaluations',null=True,blank=True,
                               verbose_name='所属医生')
    user = models.ForeignKey(UserModel, on_delete=models.DO_NOTHING, related_name='evaluations',null=True,blank=True,
                             verbose_name='用户')
    content = RichTextUploadingField(default='',verbose_name='评价内容')
    score = models.FloatField(default=5.0, verbose_name='评分')

    class Meta:
        db_table = 'doctor_evaluation'
        verbose_name = '医生评价表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.content

# 医生回答
# class DoctorAnswerModel(BaseModel):
#     question = models.ForeignKey('QuestionModel', on_delete=models.DO_NOTHING, related_name='answers', null=True, blank=True, verbose_name='所属问题')
#     content = RichTextUploadingField(default='', verbose_name='回答内容')
#     like_count = models.IntegerField(default=0, verbose_name='点赞数')
#     doctor = models.ForeignKey('DoctorModel', on_delete=models.DO_NOTHING, related_name='answers', null=True, blank=True  , verbose_name='所属医生')
#     user = models.ForeignKey(UserModel, on_delete=models.DO_NOTHING, related_name='answers', null=True, blank=True,
#                              verbose_name='用户')
#
#     class Meta:
#         db_table = 'doctor_answer'
#         verbose_name = '医生回答表'
#         verbose_name_plural = verbose_name


class DoctorModel(BaseModel):
    TITLE_CHOICES = (
        (0, '住院医师'),
        (1, '主治医师'),
        (2, '副主任医师'),
        (3, '主任医师'),
    )
    name = models.CharField(max_length=20, verbose_name='医生姓名')
    title = models.SmallIntegerField(choices=TITLE_CHOICES, default=0, verbose_name='医生职称')
    avatar = StdImageField(upload_to='doctor/%Y/', blank=True, null=True, verbose_name='医生头像'
                           , variations={
            'thumb_600x800': {'width': 600, 'height': 800},
            'thumb_300x400': {'width': 300, 'height': 400},
            'thumb_100x100': {'width': 100, 'height': 100}
        }

                           )
    introduction = RichTextUploadingField(default='', blank=True, null=True, verbose_name='医生简介')
    specialty = RichTextUploadingField(default='', blank=True, null=True, verbose_name='医生擅长')
    department = models.ForeignKey('DepartmentModel', on_delete=models.DO_NOTHING, related_name='doctors', null=True,
                                   blank=True, verbose_name='所属科室')
    hospital = models.ForeignKey('HospitalModel', on_delete=models.DO_NOTHING, related_name='doctors', null=True,
                                 blank=True, verbose_name='所属医院')
    consultation_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name='挂号费')
    rating = models.DecimalField(max_digits=3, decimal_places=1, default=5.0, verbose_name='评分')
    consultation_count = models.IntegerField(default=0, verbose_name='咨询人数')
    avg_response_time = models.IntegerField(default=0, verbose_name='平均响应时间(分钟)')
    city = models.ForeignKey('CityModel', on_delete=models.DO_NOTHING, related_name='doctors', null=True,blank=True,verbose_name='所属城市')
    class Meta:
        db_table = 'doctor'
        verbose_name = '医生表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def avatar_preview(self):
        if self.avatar:
            return mark_safe(
                f"<img alt='avatar' src='{self.avatar.thumb_100x100.url}' style='border-radius:100%'></img>")
        return ''

    avatar_preview.short_description = '医生头像预览'

    avatar_preview.allow_tags = True

    avatar_preview.admin_order_field = 'avatar'






# 疾病
class TreatmentModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='疾病名称')
    description = RichTextUploadingField(default='', blank=True, null=True, verbose_name='疾病描述')
    doctors = models.ManyToManyField('DoctorModel', related_name='treatments', verbose_name='医生')
    department = models.ForeignKey('DepartmentModel', on_delete=models.DO_NOTHING, related_name='treatments', null=True,
                                   blank=True, verbose_name='所属科室')
    remark = models.CharField(max_length=256, blank=True, null=True, verbose_name='备注')

    class Meta:
        db_table = 'treatment'
        verbose_name = '疾病表'
        verbose_name_plural = verbose_name


# 问题
class QuestionModel(BaseModel):
    avatar = models.ImageField(upload_to='question/', blank=True, null=True, verbose_name='问题头像')
    content = RichTextUploadingField(default='', blank=True, null=True, verbose_name='问题内容')
    department = models.ForeignKey('DepartmentModel', on_delete=models.DO_NOTHING, related_name='questions', null=True, blank=True, verbose_name='所属科室')
    treatment = models.ForeignKey('TreatmentModel', on_delete=models.DO_NOTHING, related_name='questions', null=True, blank=True, verbose_name='相关疾病')
    view_count = models.IntegerField(default=0, verbose_name='浏览量')
    class Meta:
        db_table = 'question'
        verbose_name = '问题表'
        verbose_name_plural = verbose_name
        ordering = ['-created_time']

    def __str__(self):
        return self.content[:20]

# 回答
class AnswerModel(BaseModel):
    question = models.ForeignKey('QuestionModel', on_delete=models.DO_NOTHING, related_name='answers', null=True,blank=True,verbose_name='问题')
    content = RichTextUploadingField(default='', blank=True, null=True, verbose_name='回答内容')
    doctor = models.ForeignKey('DoctorModel', on_delete=models.DO_NOTHING, related_name='answers', null=True,blank=True,verbose_name='回答医生')
    like_count = models.IntegerField(default=0, verbose_name='点赞数')

    class Meta:
        db_table = 'answer'
        verbose_name = '回答表'
        verbose_name_plural = verbose_name
        ordering = ['-like_count','-created_time']